package com.loadmore.common.schedule

import android.app.NotificationManager
import android.app.PendingIntent
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.media.Ringtone
import android.media.RingtoneManager
import android.net.Uri
import androidx.core.app.NotificationCompat
import android.text.TextUtils
import com.blankj.utilcode.util.AppUtils
import com.loadmore.common.App
import com.loadmore.common.R
import com.loadmore.common.log.LogUtil
import com.loadmore.common.model.Schedule
import com.loadmore.common.router.Router
import com.loadmore.common.router.RouterPath
import java.util.*


internal class AlarmReceiver : BroadcastReceiver() {

    /**
     * 提醒类型 ["不提醒", "事件发生时", "提前10分钟","提前30分钟","提前一小时","提前一天"]
     * 重复类型 ["一次性活动", "每分钟","每小时","每天", "每个工作日","每周","每月","每年"]
     **/
    override fun onReceive(context: Context, intent: Intent?) {
        if (intent != null && !TextUtils.isEmpty(intent.action)) {
            LogUtil.d(intent.action)
            if (intent.action!!.startsWith(AlarmActions.alarm_clock)) {
                val id = intent.extras.getLong("id")
                val data = App.daoSession.getDao(Schedule::class.java).loadByRowId(id)
                if (data != null) {
                    val schedule = data as Schedule



                    val calendar = Calendar.getInstance()

                    val startcalendar= Calendar.getInstance()

                    val hourOfDay = startcalendar.get(Calendar.HOUR_OF_DAY)
                    val minute = startcalendar.get(Calendar.MINUTE)



                    if (AppUtils.isAppForeground()) {
                        Router.activty(RouterPath.Alarm, schedule)
                    }else{
                        val intent = Intent(context,AlarmActivity::class.java)
                        intent.action = intent.action
                        intent.putExtra("data",schedule)
                        val pendingIntent = PendingIntent.getActivity(context,0,intent,PendingIntent.FLAG_UPDATE_CURRENT)
                        val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
                        val notification = NotificationCompat.Builder(context).setContentIntent(pendingIntent)
                                .setContentTitle(schedule.name)
                                .setAutoCancel(true)
                                .setContentText(schedule.brief)
                                .setSmallIcon(R.mipmap.icon_live)
                                .setLargeIcon(BitmapFactory.decodeResource(context.resources,R.mipmap.icon_schedule_notification))
                                .build()
                        notificationManager.notify(id.toInt(),notification)

                    }
                    var alert:Uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
                    if(alert != null) {
                        var ringtoneSound:Ringtone  = RingtoneManager.getRingtone(context, alert)
                        ringtoneSound?.play()
                    }


                    if (System.currentTimeMillis() > schedule.end_time)
                        return
                    when(schedule.repeatType) {
                        0->{

                        }
                        1->{
                            calendar.add(Calendar.MINUTE,1)
                            AlarmUtil.setAlarm(context,id,calendar.timeInMillis,0,schedule.remindType,schedule.repeatType)
                        }
                        2->{
                            calendar.add(Calendar.HOUR_OF_DAY,1)
                            calendar.set(Calendar.MINUTE,minute)
                            AlarmUtil.setAlarm(context,id,calendar.timeInMillis,0,schedule.remindType,schedule.repeatType)
                        }
                        3->{

                            calendar.set(Calendar.HOUR_OF_DAY,hourOfDay)
                            calendar.set(Calendar.MINUTE,minute)
                            AlarmUtil.setAlarm(context,id,calendar.timeInMillis,0,schedule.remindType,schedule.repeatType)
                        }
                        4->{
                            AlarmUtil.setAlarm(context,id,0,0,0,0)
                        }
                        5->{
                            AlarmUtil.setAlarm(context,id,0,0,0,0)
                        }
                    }
                }
            }
        }
    }
}